package com.baoy.cloud.xxx.sso.web;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.baoy.cloud.xxx.common.base.BaseController;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.RestController;
import com.baoy.cloud.xxx.common.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baoy.cloud.xxx.sso.service.UserService;
import com.baoy.cloud.xxx.common.JSONResult;
import com.baoy.cloud.xxx.sso.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @author baoy.bao
 * @description : User 控制器
 * ---------------------------------
 * @since 2018-07-27
 */
@Slf4j
@RestController
public class UserController extends BaseController {

    @Autowired
    public UserService userService;

    /**
     * @description : 获取分页列表
     * @author : baoy.bao
     * @since :  2018-07-27
     */
    @PostMapping("/users")
    public JSONResult list(@RequestParam(value = "pageNo") Integer pageNo,
                           @RequestParam(value = "pageSize") Integer pageSize,
                           User param) {
        JSONResult resJson = new JSONResult();
        try {
            Page<User> page = new Page<User>(pageNo, pageSize);
            userService.selectPage(page, new EntityWrapper<User>(param));
            resJson.setData(page);
            resJson.setSuccess(true);
        } catch (Exception e) {
            resJson.setSuccess(false);
            resJson.setMessage("异常信息:{" + e.getClass().getName() + "}");
            log.error("异常信息:{}", e.getMessage());
        }
        return resJson;
    }

    /**
     * @description : 通过id获取User
     * @author : baoy.bao
     * @since :  2018-07-27
     */
    @GetMapping(value = "/user/{id}")
    public JSONResult getUserById(@PathVariable("id") String id) {
        JSONResult resJson = new JSONResult();
        try {
            User param = userService.selectById(id);
            resJson.setData(param);
            resJson.setSuccess(true);
        } catch (Exception e) {
            resJson.setSuccess(false);
            resJson.setMessage("异常信息:{" + e.getClass().getName() + "}");
            log.error("异常信息:{}", e.getMessage());
        }
        return resJson;
    }

    /**
     * @description : 通过id删除User
     * @author : baoy.bao
     * @since :  2018-07-27
     */
    @DeleteMapping(value = "/user/{id}")
    public JSONResult deleteUserById(@PathVariable("id") String id) {
        JSONResult resJson = new JSONResult();
        try {
            resJson.setSuccess(userService.deleteById(id));
        } catch (Exception e) {
            resJson.setSuccess(false);
            resJson.setMessage("异常信息:{" + e.getClass().getName() + "}");
            log.error("异常信息:{}", e.getMessage());
        }
        return resJson;
    }

    /**
     * @description : 修改User
     * @author : baoy.bao
     * @since :  2018-07-27
     */
    @PutMapping(value = "/user")
    public JSONResult saveUser(User param) {
        JSONResult resJson = new JSONResult();
        try {
            resJson.setSuccess(userService.insertOrUpdate(param));
        } catch (Exception e) {
            resJson.setSuccess(false);
            resJson.setMessage("异常信息:{" + e.getClass().getName() + "}");
            log.error("异常信息:{}", e.getMessage());
        }
        return resJson;
    }

    /**
     * @description : 添加User
     * @author : baoy.bao
     * @since :  2018-07-27
     */
    @PostMapping(value = "/user")
    public JSONResult addUser(User param) {
        JSONResult resJson = new JSONResult();
        try {
            resJson.setSuccess(userService.insert(param));
        } catch (Exception e) {
            resJson.setSuccess(false);
            resJson.setMessage("异常信息:{" + e.getClass().getName() + "}");
            log.error("异常信息:{}", e.getMessage());
        }
        return resJson;
    }
}